#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
double inputnum=0;
double swapnum=0;
double outnum=0;
int flage=0;
int cmd_flage=0;
Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
//    connect(ui->num_button_eq,&QPushButton::clicked,ui->lcdNumber->display(inputnum),);
    ui->textBrowser->setText("");
}

Widget::~Widget()
{
    delete ui;
}
void sousa_keisan()
{
    if (swapnum!=0){
        swapnum=swapnum*10+inputnum;
    }else{
        swapnum=inputnum;
    }
//    qDebug()<<inputnum;
//    qDebug()<<swapnum;
}
void Widget::on_num_button7_clicked()
{
      inputnum=7;
      sousa_keisan();
      ui->lcdNumber->display(swapnum);
}



void Widget::on_num_button8_clicked()
{
    inputnum=8;
    sousa_keisan();
    ui->lcdNumber->display(swapnum);
}

void Widget::on_num_button9_clicked()
{
    inputnum=9;
    sousa_keisan();
    ui->lcdNumber->display(swapnum);
}


void Widget::on_num_button4_clicked()
{
    inputnum=4;
    sousa_keisan();
    ui->lcdNumber->display(swapnum);
}


void Widget::on_num_button5_clicked()
{
    inputnum=5;
    sousa_keisan();
    ui->lcdNumber->display(swapnum);
}


void Widget::on_num_button6_clicked()
{
    inputnum=6;
    sousa_keisan();
    ui->lcdNumber->display(swapnum);
}



void Widget::on_num_button1_clicked()
{
    inputnum=1;
    sousa_keisan();
    ui->lcdNumber->display(swapnum);
}


void Widget::on_num_button2_clicked()
{
    inputnum=2;
    sousa_keisan();
    ui->lcdNumber->display(swapnum);
}


void Widget::on_num_button3_clicked()
{
    inputnum=3;
    sousa_keisan();
    ui->lcdNumber->display(swapnum);
}




void Widget::on_num_button0_clicked()
{
    inputnum=0;
    sousa_keisan();
    ui->lcdNumber->display(swapnum);
}


void Widget::on_num_button_add_clicked()
{
    cmd_flage=1;
    outnum=swapnum;
    swapnum=0;
    flage=1;
    ui->lcdNumber->display(0);
    ui->textBrowser->setText("+");
}

void Widget::on_num_button_re_clicked()
{
    cmd_flage=2;
    outnum=swapnum;
    swapnum=0;
    flage=1;
    ui->lcdNumber->display(0);
    ui->textBrowser->setText("-");
}
void Widget::on_num_button_mul_clicked()
{
    cmd_flage=3;
    outnum=swapnum;
    swapnum=0;
    flage=1;
    ui->lcdNumber->display(0);
    ui->textBrowser->setText("*");
}

void Widget::on_num_button_div_clicked()
{
    cmd_flage=4;
    outnum=swapnum;
    swapnum=0;
    flage=1;
    ui->lcdNumber->display(0);
    ui->textBrowser->setText("/");
}
void Widget::on_num_button_clear_clicked()
{
    inputnum=0;
    swapnum=0;
    outnum=0;
    flage=0;
    cmd_flage=0;
    ui->lcdNumber->display(inputnum);
    ui->textBrowser->setText("");
}
void Widget::on_num_button_eq_clicked()
{
    if (flage==1){
    switch(cmd_flage){
        case 1  :
           outnum=swapnum+outnum;
           break;
        case 2  :
           outnum=swapnum-outnum;
           break;
        case 3  :
            outnum=swapnum*outnum;
            break;
        case 4  :
            if (outnum==0){
                ui->textBrowser->setText("E");
            }else{
//                swapnum=swapnum*1.0;
                qDebug()<<outnum;
                outnum=outnum/swapnum;
            }
            break;
    }
    ui->lcdNumber->display(outnum);
    }else{
    ui->lcdNumber->display(inputnum);
    }
//    qDebug()<<flage;
    ui->textBrowser->setText("");
    inputnum=0;
    swapnum=0;
    outnum=0;
    flage=0;
    cmd_flage=0;
}
